package protect.notfinish;
/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/9/22 21:55
 * @package protect.week04
 * @description 合并两个有序链表
 * 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 */

public class Ch02_MergeTwoLists {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode p = list1;
        ListNode q = list2;
        ListNode  list3= new ListNode(0);
        ListNode t = list3;

        while (p != null && q != null){
            if (p.val < q.val){
                t.next = new ListNode(p.val);
                p = p.next;
            }else {
                t.next = new ListNode(q.val);
                q = q.next;
            }

            t = t.next;
        }

        if (p.next != null){
            t.next = p;
        }

        if (q.next != null){
            t.next = q;
        }

        list3 = list3.next;
        return list3;

    }

}


class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
